<template>
  <div ref="appRef" class="my-container-box">
    <table-page-content
      :loading="tableObj.loading"
      :table-data="tableObj.tableList"
      :table-headers="tableObj.tableHeaders"
      :table-height="tableHeight"
      :page-config="searchObj.pageConfig"
      @page-size-change="pageSizeChange"
      @current-page-change="currentPageChange"
    >
      <template #type="{ row }">
        {{ getDicsName(tableObj.toDoListTypeDics, row.type) }}
      </template>
      <template #operate="{ row }">
        <div class="table-btn-box">
          <el-button v-permission="{ action: 'detail' }" link @click="handleDetail(row)"
            ><i class="iconfont icon-shijian"></i> <span class="text">处理</span></el-button
          >
        </div>
      </template>
    </table-page-content>
  </div>
</template>

<script lang="ts" setup name="Index">
import { useProxy } from '@/utils/proxy'
import { useTableHeight } from '@/hooks/tableHeight'
import { pageConfig } from '@/utils/config'
import { getPower, getDicsName } from '@/utils/index'
import { getDicsList, DicsObj } from '@/hooks/dicsData'
import useMenuStore from '@/store/modules/menu'
const { appRef, tableHeight } = useTableHeight()
const { proxy } = useProxy()
const menuStore = useMenuStore()
const menuList = computed(() => getMenuChild(menuStore.sidebarRouters))
const urlType = {
  EDRB: 1,
  EDRA: 2,
  EAMI: 4
}
const urlAddress = {
  EDRB: '/enterpriseReport/debtAudit',
  EDRA: '/enterpriseReport/debtSendBack',
  EAMI: '/systemFunction/areaMove'
}
// 搜索对象
const searchObj = reactive({
  searchForm: { enterpriseName: null, areaCode: null },
  pageConfig: { ...pageConfig }
})
const tableObj = reactive({
  loading: false,
  toDoListTypeDics: [] as TselectOptions[],
  tableHeaders: [
    {
      name: '待办类型',
      key: 'type',
      wid: 100,
      slotName: 'type'
    },
    {
      name: '申请编号',
      key: 'billNo',
      wid: 100
    },
    {
      name: '企业编号',
      key: 'enterpriseId',
      wid: 100
    },
    {
      name: '企业名称',
      key: 'enterpriseName',
      wid: 100
    },
    {
      name: '审批状态',
      key: 'workflowStatus',
      wid: 100
    },
    {
      name: '操作',
      wid: 100,
      fixed: 'right',
      showTooltip: false,
      slotName: 'operate'
    }
  ],
  tableList: []
})
// 获取列表数据
const getList = () => {
  tableObj.loading = true
  tableObj.tableList = []
  proxy.$api
    .getMessageList<any>({
      ...searchObj.searchForm,
      ...searchObj.pageConfig
    })
    .then((res) => {
      tableObj.tableList = res.data
      searchObj.pageConfig.total = res.total
      tableObj.loading = false
    })
    .catch(() => {
      tableObj.loading = false
    })
}

// 处理菜单数组
const getMenuChild = (list: any[]) => {
  let arr: any[] = []
  list.forEach((v) => {
    if (v.children) {
      v.children.forEach((item: any) => arr.push(item))
    } else {
      arr.push(v)
    }
  })
  return arr
}
// 查看详情
const handleDetail = (row: any) => {
  if (row.type == 'EAAE') {
    let index = menuList.value.findIndex((v) => v.path == '/basicData/unitAccountAudit')
    if (index != -1) {
      proxy.$router.push({ path: '/basicData/unitAccountAudit' })
    } else {
      proxy.$message.msgError('无菜单权限,无法处理')
    }
  } else {
    let powerType = '0'
    let index = tableObj.toDoListTypeDics.findIndex((v) => v.code == row.type)
    let code = tableObj.toDoListTypeDics[index].code as 'EAMI' | 'EDRA' | 'EDRB'
    const urlIndex = menuList.value.findIndex((v) => v.path == urlAddress[code])
    if (urlIndex != -1 && getPower('check', true, menuList.value[urlIndex].meta.rights || [])) {
      powerType = '1'
    }
    proxy.$router.push({
      path:
        code == 'EAMI'
          ? '/systemFunction/areaMoveDetail/' + row.id
          : '/enterpriseReport/reportDetail/' + row.id,
      query: { type: '1', menuType: urlType[code], power: powerType }
    })
  }
}
// 表格切换page-size
const pageSizeChange = (val: number) => {
  searchObj.pageConfig.pageSize = val
  searchObj.pageConfig.pageIndex = 1
  getList()
}
// 切换分页
const currentPageChange = (val: number) => {
  searchObj.pageConfig.pageIndex = val
  getList()
}
const init = () => {
  getDicsList(DicsObj.toDoListType).then((res: TselectOptions[]) => {
    tableObj.toDoListTypeDics = res
  })
  getList()
}
init()
</script>

<style lang="scss" scoped></style>
